package org.lw.springbootlw.department.service.impl;



import org.lw.springbootlw.common.Entity.Department;
import org.lw.springbootlw.common.exception.DepartmentNotFoundException;
import org.lw.springbootlw.common.mapper.DepartmentMapper;
import org.lw.springbootlw.department.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Transactional // 确保操作是事务性的
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;  // 注入 Mapper

    @Override
    public Department getDepartmentById(Long id) {
        return departmentMapper.getDepartmentById(id);
    }

    @Override
    public Optional<Department> getOptionalDepartmentById(Long id) {
        return Optional.ofNullable(departmentMapper.getDepartmentById(id));
    }

    @Override
    public List<Department> getAllDepartments() {
        // 实现获取所有部门的逻辑
        return departmentMapper.getAllDepartments();
    }

    @Override
    public void createDepartment(Department department) {
        // 实现创建新部门的逻辑
        departmentMapper.insertDepartment(department);
    }

    @Override
    public void updateDepartment(Department department) {
        // 实现更新部门信息的逻辑
        departmentMapper.updateDepartment(department);
    }

    @Override
    public void deleteDepartment(Long id) {
        // 实现删除部门的逻辑
        if (!departmentMapper.existsById(id)) {
            throw new DepartmentNotFoundException("部门不存在");
        }
        departmentMapper.deleteDepartment(id);
    }
}

